{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding: utf-8\n",
    "import re\n",
    "import requests\n",
    "import pandas as pd\n",
    "\n",
    "def paByPage(page):\n",
    "\theaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'}\n",
    "\turl = 'http://bang.dangdang.com/books/bestsellers/1-' + str(page) \n",
    "\tresponse = requests.get(url, headers = headers)\n",
    "\tresult = response.text\n",
    "\t# print(result)\n",
    "\n",
    "\tr_title = '<a href=\".*?\" target=\"_blank\" title=\"(.*?)\">.*?</a>'\n",
    "\tr_rank = '<div class=\"list_num.*?\">(\\d*).</div>'\n",
    "\tr_price_n = '<div class=\"price\">.*?<span class=\"price_n\">&yen;(.*?)</span>'\n",
    "\tr_price_r = '<div class=\"price\">.*?<span class=\"price_r\">&yen;(.*?)</span>'\n",
    "\ttitles = re.findall(r_title, result)\n",
    "\t# print(titles)\n",
    "\tranks = re.findall(r_rank, result)\n",
    "\t# print(ranks)\n",
    "\tprice_n = re.findall(r_price_n, result, re.S)\n",
    "\tprice_r = re.findall(r_price_r, result, re.S)\n",
    "\t# print(price_n)\n",
    "\t# print(price_r)\n",
    "\n",
    "\tdata = {'rank': ranks, 'title': titles, 'price': price_r, 'price_discount': price_n}\n",
    "\tdata = pd.DataFrame(data)\n",
    "\t# print(data)\n",
    "\t# data.to_excel('当当图书销量排行榜.xlsx', index=False)\n",
    "\treturn data\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_data = pd.DataFrame()\n",
    "for i in range(1,6):\n",
    "\tall_data = all_data.append(paByPage(i))\n",
    "all_data.to_excel('当当图书销量排行榜.xlsx', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
